home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: watserv3.uwaterloo.ca!news
- From: bcrwhims@uwaterloo.ca (Carsten Whimster)
- Subject: Re: Settle a bet please
- Message-ID: <Dp11Bx.2o7@watserv3.uwaterloo.ca>
- Sender: news@watserv3.uwaterloo.ca
- Nntp-Posting-Host: cnts2p06.uwaterloo.ca
- Reply-To: carsten_whimster@iqpac.com (Carsten Whimster)
- Organization: EDM/2
- X-Newsreader: IBM NewsReader/2 v1.2.5
- References: <4jfopb$o9n@news1.sympatico.ca>
- Date: Fri, 29 Mar 1996 11:31:09 GMT
-
- In <4jfopb$o9n@news1.sympatico.ca>, Gisele Swinson <gisele.swinson@sympatico.ca> writes:
- >In C language, how do they calculate the length of an array.
- >
- >example
- >
- >To declare a string "My Name"
- >
- >is it char name[7] = "My Name"
- >or
- >is it chat Name[8] = "My Name"
- ^ hmm
- >There is a battle in my class whether to include the NULL in the
- >array size.
-
- How about
- char Name[8] = "My Name";
- :)
- or just
- char Name[] = "My Name";
-
- Seriously, you should be able to figure this out by experimentation. Try
- it, and use a debugger to examine memory, or even just print out the
- eight chars starting at the 'M'. Make sure that you allocate the next
- few bytes, just in case. It probably wouldn't be necessary, but
- theoretically, those could be the last few bytes that the program has
- access to. Try this:
-
- int i; char Name[7] = "My Name";
- char More[] = "AAAAAAA";
-
- for (i = 0; Name[i] != '\0'; i++)
- putchar(Name[i]);
-
- If you loop until you see a NULL, you might get "My NameAAAAAAA". There
- may be some compiler variation there, but I am not sure about those.
- I am only recently getting relatively familiar with C as a language,
- never mind the platform dependencies. Actually, on my machine (OS/2 and
- IBM VAC++), it prints out:
-
- My Name\à
-
- So obviously a NULL is needed. Interestingly, the "AAAAAAA" string is
- not placed right after the Name string on my machine. Does anyone know
- what the compiler might be doing here?
-
- Carsten Whimster -- carsten_whimster@iqpac.com
- -- EDM/2 Associate Editor and Book reviewer
- EDM/2 http://www.iqpac.com/edm2/index.shtml
- Reviews http://www.iqpac.com/edm2/columns/books.shtml
- Webpage http://www.undergrad.math.uwaterloo.ca/~bcrwhims/index.html
-
-